﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using QLKDS.DTO;

namespace QLKDS.Forms
{
    public partial class frmPublisherEdit : XtraForm
    {
        #region Properties
        private PublisherManager publisherManager;
        public PublisherInfo Publisher { get; set; }
       
        #endregion

        #region Init Form
        public frmPublisherEdit()
        {
            InitializeComponent();

            // Initialize all properties of this form
            this.publisherManager = new PublisherManager();
            this.Publisher = new PublisherInfo();
            
        }

        
        private void frmPublisherEdit_Load_1(object sender, EventArgs e)
        {
            this.txtName.Text = this.Publisher.Name;        
            this.txtDescription.Text = this.Publisher.Description;
            this.dteModifiedDate.DateTime = DateTime.Now;
            this.dteModifiedDate.Properties.MaxValue = DateTime.Now;
        }
        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtName.Properties.MaxLength = 32;
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {       
            this.txtName.Text = String.Empty;
            this.txtDescription.Text = String.Empty;
            this.dteModifiedDate.DateTime = DateTime.Now;
            this.dteModifiedDate.Properties.MaxValue = DateTime.Now;
            this.txtName.Focus();
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên nhà xuất không được để trống.", ErrorType.User1);
            }
        }

       

  


        #endregion

        #region button_click

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //id = this.Customer.Id;
            var publisher = new Publisher();

            // Validation
            if (this.txtName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtName, "Tên nhà xuất bản không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepName.ClearErrors();
            }

        

            if (this.txtModifiedBy.Text.Trim().Length == 0)
            {
                this.dxepModifiedBy.SetError(this.txtModifiedBy, "Lựa chọn tên nhân viên sửa đổi");
                this.txtModifiedBy.Focus();
                return;
            }
            // End of validation
            /*****************************************************************/
            // Save into database
            publisher.Id = this.Publisher.Id;

            publisher.Name = this.txtName.Text.Trim();
            publisher.Description = this.txtDescription.Text.Trim();
            publisher.CreatedBy = this.Publisher.CreatedBy;
            publisher.CreatedDate = DateTime.Now;
            publisher.ModifiedBy = this.txtModifiedBy.Text;
            publisher.ModifiedDate = this.dteModifiedDate.DateTime;
            this.publisherManager.UpdatePublisher(publisher);
            XtraMessageBox.Show("Sửa dữ liệu thành công");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #endregion

        

   
    }
}
